package dontCare.gwtester.client.puzzlers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.SourceUrl;
import dontCare.gwtester.client.tool.UnitWidget;

public class Album2 extends UnitWidget{
	private static Album2UiBinder uiBinder = GWT.create(Album2UiBinder.class);
	interface Album2UiBinder extends UiBinder<Widget, Album2> {}

	//Puzzler 1
	@UiField TextBox input;
	@UiField Label by1;
	@UiField Label by0;
	@UiField Label byBit;
	//
	
	//Puzzler 2
	@UiField Label doubleChange;
	@UiField Label bigDecimalByDoubleChange;
	@UiField Label bigDecimalChange;
	//
	
	//Puzzler 3
	@UiField Label microsPerDay;
	@UiField Label millisPerDay;
	@UiField Label division;
	@UiField Label microsPerDayL;
	@UiField Label millisPerDayL;
	@UiField Label divisionL;
	//
	
	//Puzzler 5
	@UiField Label result;
	@UiField Label result2;
	//
	
	@UiField Label multicast;
	
	//Puzzler 8
	@UiField Label xOr0;
	@UiField Label iOrX;
	//
	
	public Album2() {
		initWidget(uiBinder.createAndBindUi(this));
		timeForChange();
		longDivision();
		joyOfHex();	
		multicast.setText(""+((int)(char)(byte) -1));
		dosEquis();
	}
	
	//Puzzler 1
	private static boolean isOddBy1(int i){
		return i%2==1;
	}
	
	private static boolean isOddBy0(int i){
		return i%2!=0;
	}
	
	private static boolean isOddByBit(int i){
		return (i&1)!=0;
	}
	
	@UiHandler("submit") void clickSubmit(ClickEvent e){
		int number = Integer.parseInt(input.getValue());
		by1.setText(number + (isOddBy1(number)?" 是奇數":" 是偶數"));
		by0.setText(number + (isOddBy0(number)?" 是奇數":" 是偶數"));
		byBit.setText(number + (isOddByBit(number)?" 是奇數":" 是偶數"));
	}
	//
	
	private void timeForChange(){
		doubleChange.setText(""+(2.00-1.10));
		bigDecimalByDoubleChange.setText(""+(new BigDecimal(2.00).subtract(new BigDecimal(1.10))));
		bigDecimalChange.setText(""+(new BigDecimal("2.00").subtract(new BigDecimal("1.10"))));
	}
	
	private void longDivision(){
		final long MICROS_PER_DAY = 24*60*60*1000*1000;
		final long MILLIS_PER_DAY = 24*60*60*1000;
		microsPerDay.setText(""+MICROS_PER_DAY); 
		millisPerDay.setText(""+MILLIS_PER_DAY);
		division.setText(""+(MICROS_PER_DAY/MILLIS_PER_DAY));
		
		final long MICROS_PER_DAY_L = 24L*60*60*1000*1000;
		final long MILLIS_PER_DAY_L = 24L*60*60*1000;
		microsPerDayL.setText(""+MICROS_PER_DAY_L); 
		millisPerDayL.setText(""+MILLIS_PER_DAY_L);
		divisionL.setText(""+(MICROS_PER_DAY_L/MILLIS_PER_DAY_L)); 
	}
	
	private void joyOfHex(){
		result.setText(""+Long.toHexString(0x100000000L + 0xcafebabe));
		result2.setText(""+Long.toHexString(0x100000000L + 0xcafebabeL));
	}
	
	private void dosEquis() {
		char x = 'X';
		int i= 0;
		xOr0.setText(""+(true?x:0));
		iOrX.setText(""+(false?i:x));
	}

	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(Album2.class));
		result.add(SourceUrl.genUiXml(Album2.class));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML("《Java Puzzlers》 Chapter 2 puzzler 集合");
	}

	@Override
	public String getName() {
		return "CH.2 Album";
	}
}
